home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / DesktopDoubler / NubApp / Event.cc next >
Text File  |  1999-06-23  |  6KB  |  387 lines

  1. #define DISABLE_LOCAL_CALLTRACE        1        // Set to 1 to disable Call Traces for this file.
  2. #define DISABLE_LOCAL_DEBUG            0        // Set to 1 to disable all debugging for this file.
  3. #include "DebugUtils.h"
  4.  
  5. #include <Dialogs.h>
  6. #include "Event.h"
  7. #include "Main.h"
  8. #include "Menu.h"
  9. #include "Window.h"
  10.  
  11.  
  12.  
  13.  
  14.  
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18.  
  19. static pascal OSErr AECoreHandler(AppleEvent *aevent,AppleEvent *areply,long refcon);
  20. static OSStatus AEGotRequiredParams(AppleEvent *aevent);
  21.  
  22. #ifdef __cplusplus
  23. }
  24. #endif
  25.  
  26.  
  27.  
  28.  
  29.  
  30. extern MenuManager            *gMenuManager;
  31. extern WindowManager        *gWindowManager;
  32.  
  33.  
  34.  
  35.  
  36.  
  37. void BaseEventManager::DoDispatch(EventRecord *event)
  38. {
  39.     if ((event->what != nullEvent) && (event->what != osEvt) && IsDialogEvent(event) &&
  40.         !((event->what == keyDown) && (event->modifiers & cmdKey)))
  41.     {
  42.         HandleDialogEventDispatch(event);
  43.     }
  44.     else
  45.     {
  46.         switch(event->what)
  47.         {
  48.             case nullEvent:
  49.                 HandleNullEventDispatch(event);
  50.                 break;
  51.             
  52.             case mouseDown:
  53.                 HandleMouseDownDispatch(event);
  54.                 break;
  55.             
  56.             case mouseUp:
  57.                 HandleMouseUpDispatch(event);
  58.                 break;
  59.             
  60.             case keyDown:
  61.                 HandleKeyDownDispatch(event);
  62.                 break;
  63.             
  64.             case keyUp:
  65.                 HandleKeyUpDispatch(event);
  66.                 break;
  67.             
  68.             case autoKey:
  69.                 HandleAutoKeyDispatch(event);
  70.                 break;
  71.             
  72.             case updateEvt:
  73.                 HandleUpdateEvtDispatch(event);
  74.                 break;
  75.             
  76.             case activateEvt:
  77.                 HandleActivateEvtDispatch(event);
  78.                 break;
  79.             
  80.             case osEvt:
  81.                 HandleOSEvtDispatch(event);
  82.                 break;
  83.             
  84.             case kHighLevelEvent:
  85.                 HandleHighLevelEventDispatch(event);
  86.                 break;
  87.         }
  88.     }
  89. }
  90.  
  91.  
  92.  
  93.  
  94.  
  95. void BaseEventManager::HandleNullEventDispatch(EventRecord *event)
  96. {
  97.     gWindowManager->DoIdleTime(event,event->where,event->modifiers);
  98.     gMenuManager->DoMenuUpdates();
  99. }
  100.  
  101.  
  102.  
  103.  
  104.  
  105. void BaseEventManager::HandleMouseDownDispatch(EventRecord *event)
  106. {
  107.     WindowPtr    window;
  108.     SInt32        part;
  109.     Window        *win;
  110.     
  111.     
  112.     part = FindWindow(event->where,&window);
  113.     switch(part)
  114.     {
  115.         case inDesk:
  116.             break;
  117.         
  118.         case inMenuBar:
  119.             HandleInMenuBarDispatch(event);
  120.             break;
  121.         
  122.         case inSysWindow:
  123.             SystemClick(event,window);
  124.             break;
  125.         
  126.         case inContent:
  127.         case inDrag:
  128.         case inGrow:
  129.         case inGoAway:
  130.         case inZoomIn:
  131.         case inZoomOut:
  132.             gWindowManager->DoClick(event->where,event->modifiers,window,part);
  133.             break;
  134.     }
  135. }
  136.  
  137.  
  138.  
  139.  
  140.  
  141. void BaseEventManager::HandleMouseUpDispatch(EventRecord *event)
  142. {
  143.     
  144. }
  145.  
  146.  
  147.  
  148.  
  149.  
  150. void BaseEventManager::HandleKeyDownDispatch(EventRecord *event)
  151. {
  152.     UInt32    selection;
  153.     UInt32    key;
  154.     Window    *win;
  155.     
  156.     
  157.     key = charCodeMask & event->message;
  158.     
  159.     // Resolve menu item command keys
  160.     if (event->modifiers & cmdKey)
  161.     {
  162.         gMenuManager->DoMenuUpdates();
  163.         selection = MenuKey(key);
  164.         if ((selection >> 16) != 0)
  165.         {
  166.             gMenuManager->DoSelect((selection >> 16),(selection & 0xFFFF));
  167.             return;
  168.         }
  169.     }
  170.     
  171.     // Give it the most eligible window
  172.     if (NULL != (win = gWindowManager->DoGetFrontWindow()))
  173.         win->DoKey(key,event->modifiers);
  174. }
  175.  
  176.  
  177.  
  178.  
  179.  
  180. void BaseEventManager::HandleKeyUpDispatch(EventRecord *event)
  181. {
  182.     
  183. }
  184.  
  185.  
  186.  
  187.  
  188.  
  189. void BaseEventManager::HandleAutoKeyDispatch(EventRecord *event)
  190. {
  191.     HandleKeyDownDispatch(event);
  192. }
  193.  
  194.  
  195.  
  196.  
  197.  
  198. void BaseEventManager::HandleUpdateEvtDispatch(EventRecord *event)
  199. {
  200.     WindowPtr    window;
  201.     Window        *win;
  202.     
  203.     
  204.     window = (WindowPtr)event->message;
  205.     win = gWindowManager->DoGetWindow(window);
  206.     if (win != NULL)
  207.         win->DoUpdate();
  208. }
  209.  
  210.  
  211.  
  212.  
  213.  
  214. void BaseEventManager::HandleActivateEvtDispatch(EventRecord *event)
  215. {
  216.     WindowPtr    window;
  217.     
  218.     
  219.     window = (WindowPtr)event->message;
  220.     gWindowManager->DoActivation(window,event->modifiers & activeFlag);
  221.     gMenuManager->DoMenuUpdates();
  222. }
  223.  
  224.  
  225.  
  226.  
  227.  
  228. void BaseEventManager::HandleOSEvtDispatch(EventRecord *event)
  229. {
  230.     // Is this a suspend/resume event?
  231.     if ((event->message & 0xFF000000) == 0x01000000)
  232.     {
  233.         gWindowManager->DoSuspendResume(event,!(event->message & 1));
  234.         gMenuManager->DoMenuUpdates();
  235.     }
  236. }
  237.  
  238.  
  239.  
  240.  
  241.  
  242. void BaseEventManager::HandleHighLevelEventDispatch(EventRecord *event)
  243. {
  244.     ProcessAppleEvent(event);
  245. }
  246.  
  247.  
  248.  
  249.  
  250.  
  251. void BaseEventManager::HandleDialogEventDispatch(EventRecord *event)
  252. {
  253.     WindowPtr    window;
  254.     DialogPtr    dialog;
  255.     short        item;
  256.     Window        *win;
  257.     
  258.     
  259.     win = NULL;
  260.     switch(event->what)
  261.     {
  262.         case mouseDown:
  263.         case mouseUp:
  264.             FindWindow(event->where,&window);
  265.             win = gWindowManager->DoGetWindow(window);
  266.             break;
  267.         
  268.         case keyDown:
  269.         case keyUp:
  270.         case autoKey:
  271.             win = gWindowManager->DoGetFrontWindow();
  272.             break;
  273.         
  274.         case updateEvt:
  275.             window = (WindowPtr)event->message;
  276.             win = gWindowManager->DoGetWindow(window);
  277.             break;
  278.             
  279.         case activateEvt:
  280.             window = (WindowPtr)event->message;
  281.             win = gWindowManager->DoGetWindow(window);
  282.             HandleActivateEvtDispatch(event);
  283.             break;
  284.     }
  285.     
  286.     if (win)
  287.         win->DoDialogEvent(event);
  288.     else
  289.         DialogSelect(event,&dialog,&item);
  290. }
  291.  
  292.  
  293.  
  294.  
  295.  
  296. void BaseEventManager::HandleInMenuBarDispatch(EventRecord *event)
  297. {
  298.     UInt32    selection;
  299.     
  300.     
  301.     gMenuManager->DoMenuUpdates();
  302.     selection = MenuSelect(event->where);
  303.     if ((selection >> 16) != 0)
  304.         gMenuManager->DoSelect((selection >> 16),(selection & 0xFFFF));
  305. }
  306.  
  307.  
  308.  
  309.  
  310.  
  311. void InitAppleEvents(void)
  312. {
  313.     AEEventHandlerUPP    AECoreProc;
  314.     
  315.     
  316.     // Install the core AppleEvent handlers.
  317.     AECoreProc = NewAEEventHandlerProc(AECoreHandler);
  318.     AEInstallEventHandler(kCoreEventClass,kAEOpenApplication,AECoreProc,kAEOpenApplication,false);
  319.     AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments,AECoreProc,kAEOpenDocuments,false);
  320.     AEInstallEventHandler(kCoreEventClass,kAEPrintDocuments,AECoreProc,kAEPrintDocuments,false);
  321.     AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,AECoreProc,kAEQuitApplication,false);
  322. }
  323.  
  324.  
  325.  
  326.  
  327.  
  328. void ProcessAppleEvent(EventRecord *event)
  329. {
  330.     AEProcessAppleEvent(event);
  331. }
  332.  
  333.  
  334.  
  335.  
  336.  
  337. pascal OSErr AECoreHandler(AppleEvent *aevent,AppleEvent *areply,long refcon)
  338. {
  339.     // Dispatch via refcon.
  340.     switch(refcon)
  341.     {
  342.         case kAEOpenApplication:
  343.             break;
  344.         
  345.         case kAEOpenDocuments:
  346.             break;
  347.         
  348.         case kAEPrintDocuments:
  349.             break;
  350.         
  351.         case kAEQuitApplication:
  352.             InitiateAppQuit();
  353.             break;
  354.     }
  355.     
  356.     // Make sure event was properly handled.
  357.     return (SInt16)AEGotRequiredParams(aevent);
  358. }
  359.  
  360.  
  361.  
  362.  
  363.  
  364. OSStatus AEGotRequiredParams(AppleEvent *aevent)
  365. {
  366.     DescType    returnedType;
  367.     Size        actualSize;
  368.     OSStatus    err;
  369.     
  370.     
  371.     // Make sure we extracted all the required parameters.
  372.     err = AEGetAttributePtr(aevent,keyMissedKeywordAttr,typeWildCard,&returnedType,nil,0,&actualSize);
  373.     if (err == errAEDescNotFound)
  374.     {
  375.         // Got all required parameters.
  376.         return noErr;
  377.     }
  378.     else if (err != noErr)
  379.     {
  380.         // Missed a required parameter.
  381.         return errAEEventNotHandled;
  382.     }
  383.     
  384.     // AEGetAttributePtr failed: weird!
  385.     return err;
  386. }
  387.